package com.weme.common.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.weme.common.R
import com.weme.common.util.dp2px
import dev.kratos.api.user_server.v1.Media
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

class BigImgIndicator @JvmOverloads constructor(
    context: Context,
    set: AttributeSet? = null,
) : MagicIndicator(context, set) {

    private var mViewPager: ViewPager2? = null


    fun attachViewPager(viewPager: ViewPager2, label: Int) {
        mViewPager = viewPager
        init(viewPager.context,label)
        bind(mViewPager)
    }

    private fun init(context: Context,label: Int) {
        val navigator = CommonNavigator(context)
        navigator.isAdjustMode = true
        //  navigator.isSkimOver = true
        navigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = label

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val commonPagerTitleView = CommonPagerTitleView(context)
                val customLayout: View = LayoutInflater.from(context).inflate(R.layout.indicator_big_img, null)
                //val view = customLayout.findViewById<View>(R.id.mView)

                commonPagerTitleView.setContentView(customLayout)
                commonPagerTitleView.onPagerTitleChangeListener = object : CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {

                    }

                    override fun onDeselected(index: Int, totalCount: Int) {

                    }

                    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                        customLayout.alpha = 0.5f  // KLog.e("tag","leavePercent"+leavePercent.toString())
                        //  customLayout.setScaleY(1.3f + (0.8f - 1.2f) * leavePercent)
                    }

                    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                        customLayout.alpha = 1f //      ALog.e("tag","enterPercent"+enterPercent.toString() + "---------"+(0.3f + (1.2f - 0.8f) * enterPercent))
                        //    customLayout.setScaleY(0.8f + (1.2f - 0.8f) * enterPercent)
                    }
                }
                commonPagerTitleView.setOnClickListener { mViewPager?.currentItem = index }
                return commonPagerTitleView
            }

            override fun getIndicator(p0: Context): IPagerIndicator? {
                val linePagerIndicator = LinePagerIndicator(context)
                linePagerIndicator.setBackgroundColor(Color.parseColor("#80FFFFFF"))
                linePagerIndicator.roundRadius = context.dp2px(0).toFloat()
                linePagerIndicator.setColors(Color.parseColor("#FFFFFF"))
                linePagerIndicator.mode = LinePagerIndicator.MODE_MATCH_EDGE
                linePagerIndicator.lineHeight = context.dp2px(4).toFloat()
                return null
            }
        }
        setNavigator(navigator)
    }
}